//
//  SDDiagnoseSkinVC.m
//  skinDetection
//
//  Created by  liangzhun on 2018/11/2.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDDiagnoseSkinVC.h"
#import "SDCompareDetcetionCell.h"
#import "SDDiagnoseSkinView.h"
#import "SDSkinAnalysisVC.h"
#import "SDDetectionDataModel.h"
#import "SDDiagnoseSkinModel.h"
#import "SDDiagnoseSkinData.h"
static NSString *const cellIden = @"SDCompareDetcetionCell";
@interface SDDiagnoseSkinVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)SDDiagnoseSkinModel *showModel;//皮肤类型细分数据

@property (nonatomic ,strong)SDDiagnoseSkinData *selectedData;//选中的皮肤类型细分的数据

@end

@implementation SDDiagnoseSkinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _navHeight.constant = SDNavBar_Height;
    [self.tableView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellReuseIdentifier:cellIden];
    [self loadData];
    
}
- (void)loadData
{
//    根据皮肤类型获取皮肤细分类的详情
    [SDNetwork getSkinTypeDetailWithParameters:[SDDetectionDataModel shareInstance].type finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        self.showModel = [SDDiagnoseSkinModel modelObjectWithDictionary:dic];
        self.selectedData = self.showModel.data.firstObject;
        [self.tableView reloadData];
    }];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230 * SDWidth375Scale;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    SDCompareDetcetionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
     SDDetectionDataModel *data = [SDDetectionDataModel shareInstance];

    indexPath.section == 0? [cell.skinImage setImage:[UIImage imageNamed:[data.detectionImageDic objectForKey:[NSString stringWithFormat:@"%lu",(unsigned long)data.type]]]]:[cell.skinImage sd_setImageWithURL:[NSURL URLWithString:self.selectedData.img]];
   
    [cell.skinBtnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *sBtn = obj;
      
        sBtn.hidden = indexPath.section == 0? idx < 3:idx > self.showModel.data.count - 1;
        if (indexPath.section == 1&&idx < self.showModel.data.count) {
            
            
            
            SDDiagnoseSkinData *data = self.showModel.data[idx];
            double right =  data.subName.length > 2? 5:15;
            [sBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, right)];
            
            [sBtn setTitle:data.subName forState:UIControlStateNormal];
            [sBtn setBackgroundColor:[self getBgColorOfSelectBtnWithSelect:self.selectedData == data]];
            UIImage *image = self.selectedData == data? [UIImage imageNamed:@"选中 拷贝 2"]:nil;
            
            [sBtn setImage:image forState:UIControlStateNormal];
            
        }
        
        [sBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            
            if (idx < self.showModel.data.count) {
                if (self.selectedData != self.showModel.data[sBtn.tag - 1]) {
                self.selectedData = self.showModel.data[sBtn.tag - 1];
                [self.tableView reloadData];
                //[self.tableView layoutIfNeeded];
                }
            }else{
                [self.navigationController popViewControllerAnimated:YES];
            }
                
        
        }];
        

        
    }];
   
    return cell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section == 0? 10:57;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return section == 0? 0.001:28;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        SDDiagnoseSkinView *headerView = [SDMethodTool getViewWithViewName:NSStringFromClass([SDDiagnoseSkinView class])];
        [headerView.subtitleLab setText:[NSString stringWithFormat:@"%@(%@)",self.selectedData.subName,self.selectedData.desc]];
        return headerView;
    }
    
    return nil;
    
}
- (UIColor *)getBgColorOfSelectBtnWithSelect:(BOOL)sel
{
    return sel == YES? RGBA(250, 207, 92, 1):RGBA(77, 72, 74, 0.3);
}
//进入分析页面
- (IBAction)goLookAnalysis:(UIButton *)sender {
    
    SDDetectionDataModel *data = [SDDetectionDataModel shareInstance];
    NSMutableArray *array = [NSMutableArray arrayWithArray:data.isDetectionArray];
    [array replaceObjectAtIndex:data.type - 1 withObject:[NSNumber numberWithBool:YES]];
    data.isDetectionArray = array;
    
    if (self.selectedData){
//    将选择的数据以皮肤类型为key存在SDDetectionDataModel的detectionReportDic中
    [data.detectionReportDic setObject:self.selectedData forKey:[NSString stringWithFormat:@"%lu",(unsigned long)data.type]];
    SDSkinAnalysisVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDSkinAnalysisVC class])];
    [self.navigationController pushViewController:vc animated:YES];
        
    }
}

- (IBAction)back:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
//-(void)setSelectedData:(SDDiagnoseSkinData *)selectedData
//{
//    _selectedData = selectedData;
//
//}
@end
